package cn.dakaqi.pingan.util;

import cn.dakaqi.pingan.services.PinganNumberService;
import cn.dakaqi.utils.http.HttpInvoker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author chunyang on 2016/3/22.
 * 平安银行银企直连接口 企业单笔资金划转
 */
@Component
@Transactional
public class PayTools
{
    private static Logger log = LoggerFactory.getLogger(PayTools.class);
    @Autowired
    PinganNumberService pinganNumberService;
    String type = "P";


    /**
     * 3.14 查询账户历史交易明细[4013]
     */
    public void queryAllTradeHistory(String account,String beginDate,String endDate,String PageNo)
    {
        try
        {
//            account = "11000593657501";
//            beginDate = "20160101";
//            endDate = "20161231";
//            PageNo = "000001";
            String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><Result><AcctNo>"+account+"</AcctNo><CcyCode>RMB</CcyCode><BeginDate>"+beginDate+"</BeginDate><EndDate>"+endDate+"</EndDate><PageNo>"+PageNo+"</PageNo><Reserve></Reserve></Result>";
            String sn = pinganNumberService.createNO(type);
            String head = MessageHead.A00101(xml,sn,"4013  ",PingAnUtil.shopNO);
            log.info(head.getBytes("UTF-8").length + "====>"+head);
            log.info(head+xml);
            String retValue = HttpInvoker.httpPostXML(PingAnUtil.B2BiUrl, head + xml);
            log.info("【查询账户历史交易明细 4013】 返回信息:" + retValue);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    /**
     * 3.3 企业当日交易明细查询[4008]
     */
    public void queryTodayTradeHistory(String account)
    {
        try
        {
//            account = "11000593657501";
            String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><Result><AcctNo>"+account+"</AcctNo><CcyCode>RMB</CcyCode><PageNo>0</PageNo></Result>";
            String sn = pinganNumberService.createNO(type);
            String head = MessageHead.A00101(xml,sn,"4008  ",PingAnUtil.shopNO);
            log.info(head.getBytes("UTF-8").length + "====>"+head);
            log.info(head+xml);
            String retValue = HttpInvoker.httpPostXML(PingAnUtil.B2BiUrl, head + xml);
            log.info("【企业当日交易明细查询 4008】 返回信息:" + retValue);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    /**
     *  3.2 企业账户余额查询 [4001]
     */
    public void queryBalance(String account)
    {
        try
        {
//            account = "11000593657501";
            String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><Result><Account>"+account+"</Account><CcyType>C</CcyType><CcyCode>RMB</CcyCode></Result>";
            String sn = pinganNumberService.createNO(type);
            String head = MessageHead.A00101(xml,sn,"4001  ",PingAnUtil.shopNO);
            log.info(head.getBytes("UTF-8").length + "====>"+head);
            log.info(head+xml);
            String retValue = HttpInvoker.httpPostXML(PingAnUtil.B2BiUrl, head + xml);
            log.info("【企业账户余额查询 4001】 返回信息:" + retValue);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    /**
     * 企业单笔资金划转 [4004]
     * @param InAcctBankName 转入银行名称
     * @param InAcctNo 转入银行账号
     * @param InAcctName 转入企业账号
     * @param TranAmount 转账金额
     */
    public void payMoney(String InAcctBankName,String InAcctNo,String InAcctName,String TranAmount)
    {

        try
        {
            String OutAcctName = "上海悠活网络科技有限公司";
            String OutAcctNo = "11015100817002";
            String CcyCode = "RMB";

//            InAcctBankName = "中国银行上海市虹梅路支行";
//            InAcctNo = "439059222738";
//            InAcctName = "上海悠活网络科技有限公司";
//            TranAmount = "1.00";



//            String OutAcctName = "平安测试保险";
//            String OutAcctNo = "11000593657501";
//            String CcyCode = "RMB";
//            InAcctBankName = "平安测试热能工程有限公司";
//            InAcctNo = "11014820381007";
//            InAcctName = "平安测试热能工程有限公司";
//            TranAmount = "1.00";


            String sn = pinganNumberService.createNO(type);
            String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><Result><ThirdVoucher>"+sn+"</ThirdVoucher><FrontLogNo></FrontLogNo><CcyCode>"+CcyCode+"</CcyCode><OutAcctName>"+OutAcctName+"</OutAcctName><OutAcctNo>"+OutAcctNo+"</OutAcctNo><InAcctBankName>"+InAcctBankName+"</InAcctBankName><InAcctNo>"+InAcctNo+"</InAcctNo><InAcctName>"+InAcctName+"</InAcctName><TranAmount>"+TranAmount+"</TranAmount><UnionFlag>0</UnionFlag><AddrFlag>1</AddrFlag><Fee1></Fee1><Fee2></Fee2></Result>";
            String head = MessageHead.A00101(xml,sn,"4004  ",PingAnUtil.shopNO);
            log.info(head.getBytes("UTF-8").length + "====>"+head);
            log.info(head+xml);
            String retValue = HttpInvoker.httpPostXML(PingAnUtil.B2BiUrl, head + xml);
            log.info("【企业单笔资金划转 4004】返回信息:" + retValue);
        } catch (Exception e)
        {
            e.printStackTrace();
            log.error(e.getMessage());
        }
    }

    /**
     * 3.10 单笔转账指令查询[4005]
     * @param OrigThirdVoucher 第三方凭证？
     * @param OrigFrontLogNo 接待日志编号？
     */
    public void querySingleTradeResult(String OrigThirdVoucher,String OrigFrontLogNo)
    {
        try
        {
            String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><Result><OrigThirdLogNo></OrigThirdLogNo><OrigThirdVoucher>"+OrigThirdVoucher+"</OrigThirdVoucher><OrigFrontLogNo>"+OrigFrontLogNo+"</OrigFrontLogNo></Result>";
            String sn = pinganNumberService.createNO(type);
            String head = MessageHead.A00101(xml,sn,"4005  ",PingAnUtil.shopNO);
            log.info(head.getBytes("UTF-8").length + "====>"+head);
            log.info(head+xml);
            String retValue = HttpInvoker.httpPostXML(PingAnUtil.B2BiUrl, head + xml);
            log.info("【单笔转账指令查询 4005】 返回信息:" + retValue);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
